package controller;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.util.List;

import javax.swing.JOptionPane;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import dao.EmailDao;

import modle.Email;
import modle.User;
import view.EmailPanel;
import view.TaskFrame;

public class EmailController implements ActionListener, ListSelectionListener {

	TaskFrame taskFrame;
	EmailPanel emailPanel;
	EmailThread emailThread;

	public EmailController(EmailPanel panel) {
		// TODO Auto-generated constructor stub
		taskFrame = panel.getTaskFrame();
		emailPanel = panel;
	}

	public void actionPerformed(ActionEvent event) {
		// TODO Auto-generated method stub
		if (event.getSource() == emailPanel.getAddButton()) {
			addEmail();
		}
		if (event.getSource() == emailPanel.getModifyButton()) {
			modifyEmail();
		}
		if (event.getSource() == emailPanel.getRemoveButton()) {
			removeEmail();
		}
	}

	private void addEmail() {
		// TODO Auto-generated method stub
		Email email = new Email();
		getEmailFromPanel(email);
		email.setUser(getUser());
		EmailDao emailDao = new EmailDao();
		if (emailDao.save(email) > 0) {
			email = emailDao.findByEmailAddress(email.getEmailAddress()).get(0);
			getUser().getEmails().add(email);
			emailPanel.getEmailTableModel().setData(getUser().getEmails());
			showMessage("Add Email Successfully!");
		} else
			showMessage("Add Email Failed!");
		emailDao.closeConnection();
	}

	private void emailTableIsChosen() {
		// TODO Auto-generated method stub
		int select = emailPanel.getEmailTable().getSelectedRow();
		List<Email> emails = getUser().getEmails();
		if (select < 0 || select >= emails.size())
			return;
		Email email = emails.get(select);
		setEmailToPanel(email);
		// set component enabled
		emailPanel.getRemoveButton().setEnabled(true);
		emailPanel.getModifyButton().setEnabled(true);
	}

	@SuppressWarnings("deprecation")
	private void getEmailFromPanel(Email email) {
		// TODO Auto-generated method stub
		email.setEmailAddress(emailPanel.getAddressField().getText());
		email.setEmailPassword(emailPanel.getPwdField().getText());
		email.setEmailPop3(emailPanel.getServerField().getText());
	}

	private User getUser() {
		return taskFrame.getUser();
	}

	public void loginSuccessfully() {
		emailPanel.getEmailTableModel().setData(getUser().getEmails());
		if (emailThread == null)
			try {
				emailThread = new EmailThread(getUser().getEmails());
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		else
			emailThread.setEmails(getUser().getEmails());
	}

	private void modifyEmail() {
		// TODO Auto-generated method stub
		int select = emailPanel.getEmailTable().getSelectedRow();
		List<Email> emails = getUser().getEmails();
		if (select < 0 || select >= emails.size())
			return;
		Email oldEmail = emails.get(select);
		Email newEmail = new Email();
		getEmailFromPanel(newEmail);
		newEmail.setEmailId(oldEmail.getEmailId());
		EmailDao emailDao = new EmailDao();
		if (emailDao.update(newEmail) > 0) {
			emails.set(select, newEmail);
			showMessage("Modify Email Successfully!");
		} else
			showMessage("Modify Email Failed!");
		emailPanel.getEmailTableModel().setData(emails);
		emailDao.closeConnection();
	}

	private void removeEmail() {
		// TODO Auto-generated method stub
		int select = emailPanel.getEmailTable().getSelectedRow();
		List<Email> emails = getUser().getEmails();
		if (select < 0 || select >= emails.size())
			return;
		Email email = emails.get(select);
		EmailDao emailDao = new EmailDao();
		if (emailDao.delete(email) > 0) {
			emails.remove(select);
			showMessage("Remove Email Successfully!");
		} else
			showMessage("Remove Email Failed!");
		emailPanel.getEmailTableModel().setData(emails);
		emailDao.closeConnection();
	}

	private void setEmailToPanel(Email email) {
		// TODO Auto-generated method stub
		emailPanel.getAddressField().setText(email.getEmailAddress());
		emailPanel.getServerField().setText(email.getEmailPop3());
		emailPanel.getPwdField().setText(email.getEmailPassword());
	}

	private void showMessage(String message) {
		JOptionPane.showMessageDialog(null, message);
	}

	public void valueChanged(ListSelectionEvent event) {
		// TODO Auto-generated method stub
		if (event.getSource() == emailPanel.getEmailTable().getSelectionModel()) {
			emailTableIsChosen();
		}
	}

}
